package com.libraryManage.service.impl;

import com.libraryManage.commons.config.Const;
import com.libraryManage.commons.utils.MVCUtil;
import com.libraryManage.commons.utils.PageUtil;

import com.libraryManage.commons.utils.ResponseEntity;
import com.libraryManage.commons.utils.SimpleResp;
import com.libraryManage.entity.BorrowDo;
import com.libraryManage.entity.BorrowingUser;
import com.libraryManage.mapper.BookManageDoMapper;
import com.libraryManage.mapper.BorrowDoMapper;
import com.libraryManage.service.BorrowService;


import com.libraryManage.vo.BorrowVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author:ZhangQing
 * @Date:2020/11/4
 */

@Service
public class BorrowServiceImpl implements BorrowService {
    @Autowired
    private BorrowDoMapper borrowDoMapper;

    @Autowired
    private BookManageDoMapper bookManageDoMapper;

    @Override
    public ResponseEntity<List<BorrowDo>> getAllBorrow() {
        PageUtil.startPage(new BorrowVo());
        List<BorrowDo> list = borrowDoMapper.findAll();
        return PageUtil.tranferPage(list);
    }

    @Override
    public BorrowDo getBorrowById(Integer id) {
        BorrowDo borrowDo = borrowDoMapper.selectByPrimaryKey(id);
        return borrowDo;
    }

    @Override
    public SimpleResp insertBorrow(BorrowVo borrowVo) {
        BorrowDo borrowDo = new BorrowDo();
        BeanUtils.copyProperties(borrowVo,borrowDo);
        int res = borrowDoMapper.insert(borrowDo);
        if(res == 1){
            return new SimpleResp(Const.STAT_SUCC,Const.MSG_SUCC);
        }else{
            return new SimpleResp(Const.STAT_SYSERR,Const.MSG_SYSERR);
        }
    }

    /**
     * 查询借阅记录
     * @param vo
     * @return
     */
    @Override
    public ResponseEntity<List<BorrowVo>> getList(BorrowVo vo) {
        PageUtil.startPage(vo);
        List<BorrowVo> list = borrowDoMapper.getList(vo);
        return PageUtil.tranferPage(list);
    }

    /**
     * 获取未处理预约借阅列表
     * @param borrowingUser
     * @return
     */
    @Override
    public ResponseEntity<List<BorrowingUser>> getBorrowingUser(BorrowingUser borrowingUser) {

        PageUtil.startPage(borrowingUser);
        List<BorrowingUser> list = borrowDoMapper.getBorrowingUser(borrowingUser);
        return PageUtil.tranferPage(list);

    }

    /**
     * 处理预约借阅
     * 可能同时出现多个人同时取消订阅同一本书，所以需要开起线程锁
     * @param userId
     * @param borrowingState 0-》借阅图书 1-》取消借阅
     * @return
     */
    @Override
    public synchronized ResponseEntity<List<BorrowingUser>> updateBorrowingState(int userId, int bookId, int borrowingState) {

        if(borrowingState == 1){
            //如果取消借阅需要增加图书库存
            bookManageDoMapper.addBookStock(bookId);
        }else {
            //如果立即借阅，则需要增加借阅记录
            Date start = new Date();
            //默认一个月
            Calendar rightNow = Calendar.getInstance();
            rightNow.setTime(start);
            rightNow.add(Calendar.MONTH,1);
            Date end = rightNow.getTime();
            borrowDoMapper.addBorrow(userId,bookId,start,end);
        }
        //不论用户怎么选择，此条记录都是处理过的，所以要更新此条记录的处理状态
        borrowDoMapper.updateBorrowingState(userId,borrowingState);
        //更新预约图书界面
        BorrowingUser borrowingUser = new BorrowingUser();
        borrowingUser.setPageNum(1);
        borrowingUser.setPageSize(10);
        return getBorrowingUser(borrowingUser);

    }


}
